﻿using System;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive
{
    public class AnnualReportRule : ComprehensiveRule<RPCompanyProfile>
    {
        public override Severity Severity => Severity.Yellow;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.Comprehensive;
        public override string Code => "YR6";
        public override string InformationalMessage => "AR year > (equal or greater then) current year -2";

        public AnnualReportRule()
        {
            For(x => x.LastAnnualReportDate).When(Validator).WithKey(x => x.Id).WithMessage("The company {0} #{1}# does not have compliant annual Report");
        }

        private bool Validator(RPCompanyProfile validatedCompanyProfile, RPCompanyProfile referenceCompanyProfile)
        {
            if (validatedCompanyProfile.LastFeedbackRequestDate == DateTime.MinValue)
                return true;

            if (validatedCompanyProfile.LastFeedbackRequestDate.Year != DateTime.UtcNow.Year)
                return true;

            if (validatedCompanyProfile.LastAnnualReportDate == DateTime.MinValue)
                return false;

            return validatedCompanyProfile.LastAnnualReportDate.Date.AddYears(2) >= DateTime.UtcNow.Date;
        }
    }
}
